﻿using Furion.DatabaseAccessor;
using Furion.DependencyInjection;
using Furion.FriendlyException;
using IndoorPosition.Server.Core.Entities;
using IndoorPosition.Server.Core.Utils;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IndoorPosition.Server.Application
{
    /// <summary>
    /// 项目管理逻辑代码
    /// </summary>
    public class ProjectService : IProjectService, ITransient
    {
        IRepository<Project> _projectRepository;
        public ProjectService(IRepository<Project> projectRepository)
        {
            _projectRepository = projectRepository;
        }

        /// <summary>
        /// 添加项目
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<int> AddAsync(ProjectInputDto input)
        {
            var exist = await _projectRepository.Where(p => p.Name == input.Name).AnyAsync();
            if (exist)
            {
                throw Oops.Oh("已经存在同名项目");
            }
            var result = await _projectRepository.InsertNowAsync(input.Adapt<Project>());
            return result.Entity.Id;
        }

        /// <summary>
        /// 删除项目
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            await _projectRepository.DeleteNowAsync(id);
        }

        /// <summary>
        /// 更新项目信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task UpdateAsync(ProjectDto input)
        {
            await _projectRepository.UpdateNowAsync(input.Adapt<Project>());
        }

        /// <summary>
        /// 分页获取项目信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PagedList<ProjectDto>> ListAsync(PageInputDto input)
        {
            var query = _projectRepository.AsQueryable();
            if (!string.IsNullOrEmpty(input.Keyword))
            {
                query = query.Where(p => p.Name.Contains(input.Keyword));
            }
            if (input.Conditions != null && input.Conditions.Count > 0)
            {
                query = query.Where(LambdaExpressionBuilder.BuildLambda<Project>(input.Conditions));
            }
            var projectPageList = await query.Include("Buildings").ToPagedListAsync(input.Page, input.Size);
            return projectPageList.Adapt<PagedList<ProjectDto>>();
        }

        /// <summary>
        /// 获取所有的项目
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<List<ProjectDto>> ListAllAsync(SearchInputDto input)
        {
            var query = _projectRepository.AsQueryable();
            if (!string.IsNullOrEmpty(input.Keyword))
            {
                query = query.Where(p => p.Name.Contains(input.Keyword));
            }
            if (input.Conditions != null && input.Conditions.Count > 0)
            {
                query = query.Where(LambdaExpressionBuilder.BuildLambda<Project>(input.Conditions));
            }
            var projectList = await query.Include("Buildings").ToListAsync();
            return projectList.Adapt<List<ProjectDto>>();
        }

        /// <summary>
        /// 上传路径文件
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        public async Task<string> UploadRouteAsync(IFormFile file)
        {
            return await FileUtil.SaveFileAsync(file, "project");
        }

        /// <summary>
        /// 下载路径文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public async Task<IActionResult> DownloadRouteAsync(string fileName)
        {
            return new FileStreamResult(FileUtil.GetStream(fileName, "project"), "application/json") { FileDownloadName = fileName };
        }
    }
}
